import config from './config'
import type { ParamsInterface } from '@/constrain/interface'
class Service {
	private api(params : ParamsInterface) {
		return new Promise((resolve, reject) => {
			const { url, data, method } = params
			type headerType = {
				'Content-Type': string,
				token?: string
			}
			const header:headerType = {
				'Content-Type':'application/json',
			}
			const token = uni.getStorageSync('token')
			token && (header.token = token)


			uni.showLoading({
				title: '正在加载...'
			})
			uni.request({
				url: config.BASEURL + url,
				data,
				method,
				header,
				success: res => {
					uni.hideLoading()
					resolve(res.data)
				},
				fail: err => {
					uni.hideLoading()
					reject(err)
				},
				complete: () => {
					uni.hideLoading()
				}
			})
		})
	}
	get(params : ParamsInterface) {
		params.method = 'GET'
		return this.api(params)
	}
	post(params : ParamsInterface) {
		params.method = 'POST'
		return this.api(params)
	}
	put(params : ParamsInterface) {
		params.method = 'PUT'
		return this.api(params)
	}
	delete(params : ParamsInterface) {
		params.method = 'DELETE'
		return this.api(params)
	}
}

export default Service